package com.android.date;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.androidx.widget.CalendarView;

import java.util.Calendar;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

    private final static String TAG = "MainActivity";

    private TextView tvUp;
    private TextView tvDown;
    private TextView tvMonth;
    private CalendarView calendarView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        calendarView = findViewById(R.id.calendarView);
        tvUp = findViewById(R.id.tvUp);
        tvDown = findViewById(R.id.tvDown);
        tvMonth = findViewById(R.id.tvMonth);

        tvMonth.setText(calendarView.getYear() + "年" + calendarView.getMonth() + "月");

        calendarView.setMonthMode(CalendarView.VERTICAL);
//        calendarView.setDisableTextColor(Color.parseColor("#F2F5Fc"));
        //设置选中区间
//        calendarView.setIntervalStart("2022-08-06");
//        calendarView.setIntervalEnd("2022-08-10");
//        //设置可选区间
//        calendarView.setMinTime("2022-08-01");
//        calendarView.setMaxTime("2022-08-20");

        calendarView.setOnCalendarChangeListener(new CalendarView.OnCalendarChangeListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onCalendarChange(CalendarView v, Date date) {
                Log.e(TAG, "选择的月份:" + v.getMonth());
                tvMonth.setText(calendarView.getYear() + "年" + calendarView.getMonth() + "月");
            }
        });
        tvUp.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View view) {

                if (calendarView.getMonth() == 1) {
                    calendarView.setYear(calendarView.getYear() - 1);
                    calendarView.setMonth(12);
                } else {
                    calendarView.setMonth(calendarView.getMonth() - 1);
                }

                tvMonth.setText(calendarView.getYear() + "年" + calendarView.getMonth() + "月");
            }
        });

        tvDown.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onClick(View view) {

                if (calendarView.getMonth() == 12) {
                    calendarView.setYear(calendarView.getYear() + 1);
                    calendarView.setMonth(1);
                } else {
                    calendarView.setMonth(calendarView.getMonth() + 1);
                }

                tvMonth.setText(calendarView.getYear() + "年" + calendarView.getMonth() + "月");
            }
        });
    }

}